#include "VSSceneManager.h"
#include "VSGraphicInclude.h"
#include "VSUpdateThread.h"
#include "VSTimer.h"
using namespace VSEngine2;
VSSceneManager * VSSceneManager::ms_pSceneManager = NULL;
VSSceneManager::VSSceneManager()
{
	
	ms_pSceneManager = this;
}
VSSceneManager::~VSSceneManager()
{
	
}

void VSSceneManager::Update(double dAppTime)
{
	struct UpdateAVObject
	{
		VSAVObject * pAVObject;
		double dAppTime;
	};
	
	for(unsigned int i = 0 ; i < m_pCamera.GetNum() ; i++)
	{

		UpdateAVObject CameraUpdate;
		CameraUpdate.pAVObject = m_pCamera[i];
		CameraUpdate.dAppTime = dAppTime;
		ENQUEUE_UNIQUE_UPDATE_COMMAND_ONEPARAMETER(VSUpdateCamera,
			UpdateAVObject,CameraUpdate,CameraUpdate,
		{
			CameraUpdate.pAVObject->UpdateAll(CameraUpdate.dAppTime); 
		})
			CameraUpdate.pAVObject->UpdateAll(CameraUpdate.dAppTime); 
		ENQUEUE_UNIQUE_RENDER_COMMAND_END
	}
	
	for (unsigned int i = 0 ;i < m_pScene.GetNum() ; i++)
	{
		m_pScene[i]->Update(dAppTime);
	}
	if (VSResourceManager::ms_bUpdateThread)
	{
		VSUpdateThreadSys::ms_pUpdateThreadSys->Begin();
/*		double Time = VSTimer::ms_pTimer->GetGamePlayTime();*/
		VSUpdateThreadSys::ms_pUpdateThreadSys->ExChange();
// 		Time = VSTimer::ms_pTimer->GetGamePlayTime() - Time;
// 		VSOutPutDebugString("Time %f\n",Time);
	}
	
	for (unsigned int i = 0 ;i < m_pScene.GetNum() ; i++)
	{
		m_pScene[i]->CollectUpdateInfo();
	}
}
void VSSceneManager::Draw(double dAppTime)
{
	
	for (unsigned int i = 0 ; i < m_pViewFamily.GetNum() ;i++)
	{
		if (m_pViewFamily[i] && m_pViewFamily[i]->m_bEnable)
		{
			m_pViewFamily[i]->Update(dAppTime);
			m_pViewFamily[i]->Draw(dAppTime);
		}
		
	}
	
	
}
VSViewFamily * VSSceneManager::GetViewFamily(const VSString & ViewFamilyName)const
{
	for (unsigned int i = 0 ; i < m_pViewFamily.GetNum() ;i++)
	{
		if (m_pViewFamily[i]->m_ViewFamilyName == ViewFamilyName)
		{
			return m_pViewFamily[i];
		}
	}
	return NULL;
}
